package com.google.code.frcteamapp;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {
	
	public static WebView webview;
	
	public static String curPage;
	
	public static String urlBase;
	
	public void loadContact() {
		String contactPage = urlBase+getString(R.string.contactPage);
		webview.loadUrl(contactPage);
		curPage = "loadContact";
	}
	
	public void refresh() {
    	Method refresh;
		try {
			refresh = this.getClass().getMethod(curPage);
		} catch (NoSuchMethodException e) {
			return;
		}
    	try {
			refresh.invoke(this);
		} catch (IllegalArgumentException e) {
			return;
		} catch (IllegalAccessException e) {
			return;
		} catch (InvocationTargetException e) {
			return;
		}
	}
	
	
	public void loadHome() {
		String homeUrl = urlBase+getString(R.string.homeUrl);
		webview.loadUrl(homeUrl);
		curPage = "loadHome";
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		webview = new WebView(this);
		setContentView(webview);
		webview.getSettings().setJavaScriptEnabled(true);
		urlBase = getString(R.string.urlBase);
		loadHome();
		super.onCreate(savedInstanceState);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.action_refresh:
	    	refresh();
	    	return true;
	    case R.id.action_home:
	    	loadHome();
	    	return true;
	    case R.id.action_contact:
	    	loadContact();
	    	return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}

}
